package org.springframework.social.showcase.service.account;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;


@Repository
public class UserAccountService implements AccountRepository{
	@Autowired
	private MongoTemplate mongoTemplate;
	
	public void setMongoTemplate(MongoTemplate mongoTemplateServ)
	{
		mongoTemplate=mongoTemplateServ;
	}
	public static final String COLLECTION_NAME = "account";
	
	public void createAccount(Account acc) {
		if (!mongoTemplate.collectionExists(Account.class)) {
			mongoTemplate.createCollection(Account.class);
		}		
		System.out.println(acc);
		mongoTemplate.insert(acc, COLLECTION_NAME);
	}

	@Override
	public Account findAccountByUsername(String username) {
		Query query = new Query();
		query.addCriteria(Criteria.where("username").is(username));
		Account acc=mongoTemplate.findOne(query, Account.class);
		return acc;
	}
	
	
}
